package com.sspku.weinotes.controller;

import com.sspku.weinotes.entity.User;
import com.sspku.weinotes.exception.IncorrectParameterException;
import com.sspku.weinotes.exception.NoSuchIdException;
import com.sspku.weinotes.exception.UserAlreadyExistsException;
import com.sspku.weinotes.exception.UserDoesNotExistException;
import com.sspku.weinotes.response.ServerResponse;
import com.sspku.weinotes.security.SecurityUtils;
import com.sspku.weinotes.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;

@RestController
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private  UserService userService;
   //根据session，获取登陆用户信息
    @GetMapping("/getLoginUser")
    public ServerResponse getLoginUser() {
        return new ServerResponse(SecurityUtils.getUser());
    }
    /**
     * 注册
     *
     * @return 注册消息
     */
    @PostMapping("/registered")
    public ServerResponse registered(String name, String username, String password, String code) {
        ServerResponse serverResponse = new ServerResponse();
        try{
            int Sol=userService.registeredUser(name, username, password, code);
            if(Sol==0) return new ServerResponse(400,"注册失败");
            else return new ServerResponse(200,"注册成功");
        }catch (IncorrectParameterException e) {
            serverResponse.setStatus(400);
            serverResponse.setMsg(e.getMessage());
        }
        return serverResponse;
    }


    /**
     * 获取验证码
     *
     * @param response
     * @param email    邮箱
     */
    @GetMapping("/get_code")
    public void getCode(HttpServletResponse response,@Param("email") String email) {
        try {
            userService.getCode(email);
            response.setStatus(202);
        } catch (MessagingException e) {
            logger.info("邮件发送错误", e);
            response.setStatus(500);
        } catch (Exception e) {
            logger.info("获取验证码失败", e);
            response.setStatus(404);
        }
    }
    /**
     * 获取忘记密码邮箱验证码
     *
     * @param email    邮箱
     * @param response {@link HttpServletResponse}
     * @return 密钥
     */
    @GetMapping("/forget_get_code")
    public Integer forgetPasswordGetCode(String email, HttpServletResponse response) {
        try {
            return userService.forgetPasswordGetCode(email);
        } catch (MessagingException e) {
            logger.info("邮件发送错误", e);
            response.setStatus(500);
        } catch (UserDoesNotExistException e) {
            logger.info("用户不存在 ", e);
            response.setStatus(404);
        } catch (Exception e) {
            logger.info("获取验证码失败", e);
            response.setStatus(400);
        }
        return -1;
    }
    /**
     * 忘记密码
     *
     * @param code     验证码
     * @param vCode    密钥
     * @param password 密码
     * @return 被重置的用户
     */
    @PostMapping("/forget_password")
    public ServerResponse forgetPassword(String code, String vCode, String password) {
        ServerResponse serverResponse = new ServerResponse();
        try {
           User user= userService.forgetPassword(code,vCode,password); //传入code，vcode和新密码
            serverResponse.setStatus(200);
            serverResponse.setMsg("修改密码成功");
            serverResponse.setData(user);
        }catch(IncorrectParameterException | UserDoesNotExistException e)
        {
            serverResponse.setStatus(404);
            serverResponse.setMsg(e.getMessage());
        }
        return serverResponse;
    }
    /**
     * 更改用户信息
     *
     * @param name     昵称
     * @param password 密码
     * @return 被修改的用户信息
     */
    @PatchMapping("/change_user_profile")
    public ServerResponse changeUserProfile(String name, String password) {
        ServerResponse serverResponse = new ServerResponse();
        try {
            //修改用户信息，需要先登录，获取uid
            serverResponse.setData(userService.changeUserProfile(SecurityUtils.getUser().getUid(), name, password));
        } catch (NoSuchIdException | IncorrectParameterException e) {
            serverResponse.setMsg(e.getMessage());
            serverResponse.setStatus(404);
        }
        return serverResponse;
    }
}
